package com.zuihai.app.mvp.ui.widget.comment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.zuihai.app.R;

import org.simple.eventbus.EventBus;

/**
 * 底部弹出评论框
 * Created by haibin
 * on 2016/11/10.
 * <p>
 * Changed by fei
 * on 2016/11/17
 *
 * @author Qiujuer
 */
@SuppressWarnings("unused")
public class BottomSheetBar {

    private View mRootView;
    private EditText mEditText;

    private Context mContext;
    private TextView mBtnCommit;
    private MyBottomDialog mDialog;//  BottomDialog 如果显示完全的bottomDialog 需使用自定义的MyBottomDialog


   // private EmojiView mEmojiView;

    private BottomSheetBar(Context context) {
        this.mContext = context;
    }

    @SuppressLint("InflateParams")
    public static BottomSheetBar delegation(Context context) {
        BottomSheetBar bar = new BottomSheetBar(context);
        bar.mRootView = LayoutInflater.from(context).inflate(R.layout.layout_bottom_sheet_comment_bar, null, false);
        bar.initView();
        return bar;
    }

    private void initView() {
        mEditText = (EditText) mRootView.findViewById(R.id.et_comment);
        mBtnCommit = (TextView) mRootView.findViewById(R.id.btn_comment);

        KeyboardUtils.showSoftInput(mEditText);
        mBtnCommit.setEnabled(false);
        mDialog = new MyBottomDialog(mContext);
        mDialog.setContentView(mRootView);
        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                KeyboardUtils.hideSoftInput(mContext, mEditText);
//                mFrameLayout.setVisibility(View.GONE);
            }
        });

        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mBtnCommit.setEnabled(s.length() > 0);
            }
        });
    }

    public void appendText(String text){
        if (!TextUtils.isEmpty(text)){
            mEditText.append(text);
        }
    }

    public void show(String hint) {
        mDialog.show();
        if (!"添加评论".equals(hint)) {
            mEditText.setHint(hint + " ");
        }
       /* mRootView.postDelayed(new Runnable() {
            @Override
            public void run() {
                KeyboardUtils.showSoftInput(mEditText);
            }
        }, 50);*/
    }

    public void dismiss() {
        KeyboardUtils.hideSoftInput(mContext, mEditText);
        mDialog.dismiss();
        EventBus.getDefault().post("","showFlComment");
    }

    public void setCommitListener(View.OnClickListener listener) {
        mBtnCommit.setOnClickListener(listener);
    }

    public void handleSelectFriendsResult(Intent data) {
        String names[] = data.getStringArrayExtra("names");
        if (names != null && names.length > 0) {
            String text = "";
            for (String n : names) {
                text += "@" + n + " ";
            }
            mEditText.getText().insert(mEditText.getSelectionEnd(), text);
        }
    }

    public EditText getEditText() {
        return mEditText;
    }

    public String getCommentText() {
        return mEditText.getText().toString().trim();
    }

    public TextView getBtnCommit() {
        return mBtnCommit;
    }


//    public void hideSyncAction() {
//        mSyncToTweetView.setVisibility(View.INVISIBLE);
//        mSyncToTweetView.setText(null);
//    }

//    public void hideMentionAction() {
//        mAtView.setVisibility(View.INVISIBLE);
//    }

    /**
     * 默认显示的
     */
    public void showSyncAction() {
//        mSyncToTweetView.setVisibility(View.VISIBLE);
//        mSyncToTweetView.setText(R.string.send_tweet);
    }

//    public void showEmoji() {
//        mSyncToTweetView.setText(R.string.tweet_publish_title);
//        mFaceView.setVisibility(View.VISIBLE);
//        mFaceView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (mEmojiView == null) {
//                    mEmojiView = new EmojiView(mContext, mEditText);
//                    mFrameLayout.addView(mEmojiView);
//                }
//                mFrameLayout.setVisibility(View.VISIBLE);
//                mEmojiView.openPanel();
//                TDevice.closeKeyboard(mEditText);
//            }
//        });
//
//        mEditText.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mFrameLayout.setVisibility(View.GONE);
//            }
//        });
//    }
}
